package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import com.sky.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author ghy
 * @version 1.0.1
 * @date 2023-04-20 11:58:13
 */
@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "公共接口")
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(@ApiParam(name = "文件对象") MultipartFile file) {
        String filePath = "";
        try {
            log.warn("{}", file);
            byte[] bytes = file.getBytes();
            String filename = file.getOriginalFilename();
            filePath = aliOssUtil.upload(bytes, UUIDUtils.getUUIDName(filename));
            log.warn("上传到OSS的文件全路径: {}", filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success(filePath);
    }
}
